<template>
  <div class="swAuMarketingTicket">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        :summary-method="summaryMethod"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="refreshChange"
        @on-load="getList"
	:cell-style="cellStyle"
      >
        <template slot="searchMenu" slot-scope="{ row, size }">
          <el-button
            :size="size"
            @click="
              () => {
                setSearchFiledsEmpty(row);
                setSearchFiledsEmpty(searchForm);
                getList();
              }
            "
            icon="el-icon-delete"
            >清空</el-button
          >
        </template>
        <template slot="menuRight">
          <el-button type="primary"  size="small" @click="submit"
            >确认</el-button
          >
        </template>
	<template slot="search" slot-scope="{row,size}" >
          <el-form-item label="差异条件" style="width: 300px">
            <el-select v-model="searchForm.diffCondition" placeholder="请选择">
	      <el-option label="请选择" value=""></el-option>
              <el-option label="不等于0" value="1"></el-option>
              <el-option label="等于0" value="0"></el-option>             
            </el-select>
          </el-form-item>
        </template>
      </avue-crud>
    </basic-container>

    <el-dialog
      title="备注信息"
      :visible.sync="dialogVisible"
      width="30%" >
      <el-form label-width="60px" v-if="dialogVisible">
        <avue-form
          v-model="formRemark"
          :option="tableOptionRemark"
        ></avue-form>
      </el-form>
      <template>
        <div class="bottom-remark">
        <el-button
          type="primary"
          @click="submitRemark()">确定
        </el-button>
        <el-button
          type="primary"
          @click="dialogVisible = false">取消
        </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getForm, postForm } from "@/api/gen/gen";
import { mapGetters } from "vuex";
import ElInput from "../../../../../node_modules/element-ui/packages/input/src/input";
import { setSearchFiledsEmpty } from "../../../../api/subwayRat/commonCheck";
import transitUtil from "../../../../api/subwayRat/transitUtil";
import preview from "./preview";
import {
  tableOption,
  getSwAuMarketingTicket,
  updateSwAuMarketingTicket,startDate, endDate,tableOptionRemark
} from "../../../../api/subwayRat/conductorDifferences/swAuConductorShort/swAuMarketingTicket";
export default {
  components: { ElInput, preview },
  name: "swAuMarketingTicket",
  data() {
    return {
      userList: [],
      selectKeys: [], //存放对像
      selectKeyVals: [], //存放值
      option: tableOption,
      listLoading: true,
      isPreview: false,
      searchForm: {
        diffCondition:'',
        tradeTime: [startDate, endDate]
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false // 是否倒序
      },
      list: [],
      dialogVisible:false,
      formRemark: {},
      tableOptionRemark: tableOptionRemark,
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {},
  methods: {
   cellStyle({ row, column, rowIndex,columnIndex }) {     
      if(8==columnIndex){
        if (row.diffMoney < 0) {
          return {
            color: "red",
          };
        }else if (row.diffMoney > 0) {
          return {
            color: "green",
          };
        }
      }     
    },
    summaryMethod: function(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const col = ["amount", "money",  "stockMoney", "diffMoney","stockAmount", "diffAmount"];
        if (col.indexOf(column.property) !== -1) {
          const values = data.map(item => Number(item[column.property]));
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          if (column.property === "money" || column.property === "stockMoney" ||column.property === "diffMoney") {
            sums[index] = (sums[index] / 100).toFixed(2);
          }
        } else {
          sums[index] = "-";
        }
      });

      return sums;
    },
    setSearchFiledsEmpty,
    refreshChange() {
      this.getList();
    },
    getSummaries(params) {
      alert(1);
    },
    submit() {
      if (this.selectKeyVals.length <= 0) {
        this.$message.warning("请至少选择一条数据操作！");
        return;
      }
   
      // for (let i = 0; i < this.selectKeys.length; i++) {
      //   if (0 != this.selectKeys[i].diffMoney) {
      //     this.$message.warning("勾选数据中存在差异不为零数据！");
      //     return;
      //   }
      // }
      this.dialogVisible = true;
      this.formRemark = {};
    },
    async submitRemark(){
      this.dialogVisible = false;
      let p = {
        remark : this.formRemark.remark,
        ids : JSON.stringify(this.selectKeyVals)
      };
      updateSwAuMarketingTicket(p).then(res => {
        if (res.data.code == 0) {
          this.$message.success("操作成功！");
          this.getList();
        } else {
          this.$message.error("操作失败！");
        }
      });
    },
    previewEditor(row) {
      this.isPreview = true;
      this.$nextTick(() => {
        this.$refs.preview.rowData = row;
      });
    },
    getList() {
      this.listLoading = true;
      const [startDate, endDate] = this.searchForm.tradeTime || [];
      Object.assign(
        this.searchForm,
        this.page,
      );
      this.searchForm.startDate = startDate;
      this.searchForm.endDate = endDate;
      this.searchForm.userCode = this.$store.getters.userInfo.userCode;
      getSwAuMarketingTicket(this.searchForm).then(response => {
        this.list = response.data.data;
        this.listLoading = false;
        this.page.total = response.data.total;
      });
    },
    handleFilter(params, done) {
      Object.assign(this.searchForm, params);
      this.getList();
      done();
    },
    selectionChange(list) {
      this.selectKeys = list;
      this.selectKeyVals = [];
      for (let i = 0; i < list.length; i++) {
        this.selectKeyVals.push(list[i].id);
      }
    }
  }
};
</script>
<style lang="scss">
.el-table {
  overflow: visible !important;
}
.bottom-remark {
  //margin-top: 25px;
  text-align: center;
}
</style>
